package util;

import java.util.List;

/**
 * 分页类
 * @author ljldn
 *
 */
public class Page<T> {
	private int rowCount;	//总数据量
	private int pageCount;	//总页数
	private int pageNum; //当前页数
	private int pageSize;	//每页数据量
	
	private int firstPage; //第一页
	private int prePage;	//上一页
	private int nextPage;	//下一页
	private int lastPage;	//最后一页
	
	private int rowNum;	//从哪条记录开始显示
	
	private int everyPageSize; //显示多少个页码
	private int everyPageStart;	//从哪个页码开始显示
	private int everyPageEnd; //显示到多少个页码
	
	private boolean hasPre;//是否有上一页
	private boolean hasNext;//是否有下一页
	
	private List<T> list;//本次查询结果
	
	public Page() {
	}

	/**
	 * 
	 * @param rowCount 总记录数
	 * @param pageNumStr 当前页数
	 * @param pageSizeStr 每页显示记录数
	 */
	public Page(int rowCount, String pageNumStr, String pageSizeStr) {
		super();
		//总数
		this.rowCount = rowCount;
		//当前页码
		this.pageNum = pageNumStr == null?1:Integer.parseInt(pageNumStr);
		//每页显示数据量
		this.pageSize = pageSizeStr == null?10:Integer.parseInt(pageSizeStr);
		//总页面数
		this.pageCount = (int) Math.ceil(rowCount*1.0/pageSize);
		
		if(pageNum > pageCount && pageCount>0){
			pageNum = pageCount;
		}
		//显示起始数据
		rowNum = (pageNum-1)*pageSize;
		
		//every
		this.everyPageSize = 5;
		this.everyPageStart = this.pageNum-this.everyPageSize/2;
		if(everyPageStart<1){
			everyPageStart = 1;
		}
		this.everyPageEnd = this.pageNum+this.everyPageSize/2;
		if(everyPageEnd > pageCount){
			everyPageEnd = pageCount;
		}
		//间距>总页数，开始页为1，结束页为最后一页
		if(everyPageSize>pageCount){
			everyPageStart = 1;
			everyPageEnd = pageCount;
		}else if(everyPageEnd-everyPageStart < everyPageSize){//起止页间距小于要显示的页数
			//开始页为1，结束页=开始页+间距
			if(everyPageStart == 1){
				everyPageEnd = everyPageSize+1;
			}else if(everyPageEnd == pageCount){
				//结束页为最后一页，开始页=结束页-间距
				everyPageStart = everyPageEnd - everyPageSize;
			}
		}
		//重新判断开始页、结束页是否超过范围
		if(everyPageStart<1){
			everyPageStart = 1;
		}
		if(everyPageEnd > pageCount){
			everyPageEnd = pageCount;
		}
		//是否有上一页
		if(pageNum >1){
			hasPre = true;
			this.firstPage = 1;
			this.prePage = pageNum -1;
		}
		//是否有下一页
		if(pageNum < pageCount){
			hasNext = true;
			this.lastPage=pageCount;
			this.nextPage=pageNum+1;
		}
	}
	
	public Page(int rowCount, int pageNum, int pageSize) {
		this(rowCount,new Integer(pageNum).toString(),new Integer(pageSize).toString());
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public int getEveryPageSize() {
		return everyPageSize;
	}

	public void setEveryPageSize(int everyPageSize) {
		this.everyPageSize = everyPageSize;
	}

	public int getEveryPageStart() {
		return everyPageStart;
	}

	public void setEveryPageStart(int everyPageStart) {
		this.everyPageStart = everyPageStart;
	}

	public int getEveryPageEnd() {
		return everyPageEnd;
	}

	public void setEveryPageEnd(int everyPageEnd) {
		this.everyPageEnd = everyPageEnd;
	}

	public boolean isHasPre() {
		return hasPre;
	}

	public void setHasPre(boolean hasPre) {
		this.hasPre = hasPre;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}
	
	
	
}
